/*
 * Copyright 2011 derek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aocore.ds1;

import com.aocore.util.BitReader;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author derek
 */
/// <summary>
/// I would like to give credits to Paul Siramy wich is probably the most influent person in d2 modding community
/// references: http://phrozenkeep.planetdiablo.gamespy.com/forum/viewtopic.php?t=724
/// http://phrozenkeep.planetdiablo.gamespy.com/forum/viewtopic.php?p=46314
/// </summary>
public class Parser
{
    /// <summary>
    /// will parse a diablo .ds1 file
    /// </summary>
    /// <param name="data">an array of byte representing the data stream of the .ds1 files.
    /// example:
    ///        System.IO.Stream myStream = openFileDialog1.OpenFile();
    ///        byte[] data = new byte[myStream.Length];
    ///        myStream.Read(data, 0, data.Length);</param>
    public Parser(byte[] data)
    {
        //dependency: MBNCSUtil.dll found at: www.valhallalegends.com
        BitReader myReader = new BitReader(data, 0);
        //read the version, this does not implement versions < 16
        Version = myReader.getInt();
        if (Version >= 16)
        {
            //read the header data
            Width = myReader.getInt();
            Height = myReader.getInt();
            Act = myReader.getInt();
            Unknown2 = myReader.getInt();
            NumFiles = myReader.getInt();
            //read the file(s) path of .dt1 and tg1 used
            for (int i = 0; i < NumFiles; i++)
                myReader.getString();
            NumWall = myReader.getInt();
            NumFloor = myReader.getInt();
            MaxLayer = (NumWall * 2) + NumFloor + 1;//this is ok?

            //there might be an error with some .ds1 that require an extra unknown layer
            //read each Layers and add them in Layers list
            for (int i = 0; i < MaxLayer; i++)
            {
                Layer currLayer = new Layer(Width, Height);
                for (int y = 0; y < Height + 1; y++)
                {
                    for (int x = 0; x < Width + 1; x++)
                    {
                        currLayer.Property0[x][y] = myReader.getByte();
                        currLayer.Property1[x][y] = myReader.getByte();
                        currLayer.Property2[x][y] = myReader.getByte();
                        currLayer.Property3[x][y] = myReader.getByte();
                        
                        System.out.print(currLayer.Property0[x][y] == 0 ? " " : "X");
                    }
                    System.out.println();
                }
                Layers.add(currLayer);
            }

            //once this is done it should be at the right index to start reading objects
            NumObjects = myReader.getInt();
            int dw0, dw1, dw2, dw3, dw4;
            for (int i = 0; i < NumObjects; i++)
            {
                dw0 = myReader.getInt();
                dw1 = myReader.getInt();
                dw2 = myReader.getInt();
                dw3 = myReader.getInt();
                dw4 = myReader.getInt();
                Objects.add(new Object(dw0, dw1, dw2, dw3, dw4));

                System.out.println(dw0 + " " + dw1 + " " + dw2 + " " + dw3 + " " + dw4);
                
                if (dw0 == 24 || dw1 == 24 || dw2 == 24 || dw3 == 24 || dw4 == 24)
                {
                    if (dw0 == 14 || dw1 == 14 || dw2 == 14 || dw3 == 14 || dw4 == 14)
                    {
                        System.out.println();
                    }
                }
            }

            System.out.println();
            System.out.println();

            int breakdasfds = 0;
            //done, if you want to read NPCPaths, then you will need to implement it
            parsed = true;
        }
    }
    //layers
    public List<Layer> Layers = new LinkedList<Layer>();

    //objects
    int NumObjects;
    public List<Object> Objects = new LinkedList<Object>();

    //other
    boolean parsed = false;
    int MaxLayer;

    //Header
    int Version;
    int Width;
    int Height;
    int Act;
    int Unknown2;
    int NumFiles;
    int NumWall;
    int NumFloor;
}
